package util;

import java.io.IOException;  

import javax.servlet.*;  
import javax.servlet.http.*; 

public class SessionFilter implements Filter {  
  
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {  
        HttpServletRequest httpRequest = (HttpServletRequest)request;  
  
        try {  
            if (!httpRequest.isRequestedSessionIdValid()) {  
                HttpServletResponse httpResponse = (HttpServletResponse)response;  
                httpResponse.sendRedirect(httpRequest.getContextPath() + "/login.xhtml");  
            } else {  
                chain.doFilter(request, response);  
            }  
        } catch (Exception e) {  
            HttpServletResponse httpResponse = (HttpServletResponse)response;  
            httpResponse.sendRedirect(httpRequest.getContextPath() + "/erro.xhtml");  
        }  
    }

	@Override
	public void destroy() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void init(FilterConfig arg0) throws ServletException {
		// TODO Auto-generated method stub
		
	}  
  }
